package com.cundong.recyclerview;

import ohos.agp.components.BaseItemProvider;
import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;

/**
 * Created by cundong on 2015/10/22.
 * RecyclerViewUtils Class
 */
public class RecyclerViewUtils {
    private RecyclerViewUtils() {
        /* Do nothing */
    }

    /**
     * setHeaderView
     *
     * @param listContainer listContainer
     * @param component component
     */
    public static void setHeaderView(ListContainer listContainer, Component component) {
        BaseItemProvider outerAdapter = listContainer.getItemProvider();

        if (outerAdapter == null) {
            return;
        }

        if (outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter) {
            HeaderAndFooterRecyclerViewAdapter headerAndFooterAdapter =
                    (HeaderAndFooterRecyclerViewAdapter) outerAdapter;
            if (headerAndFooterAdapter.getHeaderViewsCount() == 0) {
                headerAndFooterAdapter.addHeaderView(component);
            }
        }
    }

    /**
     * setFooterView
     *
     * @param listContainer listContainer
     * @param component component
     */
    public static void setFooterView(ListContainer listContainer, Component component) {
        BaseItemProvider outerAdapter = listContainer.getItemProvider();

        if (outerAdapter == null) {
            return;
        }

        if (outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter) {
            HeaderAndFooterRecyclerViewAdapter headerAndFooterAdapter =
                    (HeaderAndFooterRecyclerViewAdapter) outerAdapter;
            if (headerAndFooterAdapter.getFooterViewsCount() == 0) {
                headerAndFooterAdapter.addFooterView(component);
            }
        }
    }

    /**
     * removeFooterView
     *
     * @param listContainer listContainer
     */
    public static void removeFooterView(ListContainer listContainer) {
        BaseItemProvider outerAdapter = listContainer.getItemProvider();

        if (outerAdapter != null && outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter) {
            int footerViewCounter = ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterViewsCount();
            if (footerViewCounter > 0) {
                Component footerView = ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getFooterView();
                ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).removeFooterView(footerView);
            }
        }
    }

    /**
     * removeHeaderView
     *
     * @param listContainer listContainer
     */
    public static void removeHeaderView(ListContainer listContainer) {
        BaseItemProvider outerAdapter = listContainer.getItemProvider();

        if (outerAdapter != null && outerAdapter instanceof HeaderAndFooterRecyclerViewAdapter) {
            int headerViewCounter = ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getHeaderViewsCount();
            if (headerViewCounter > 0) {
                Component headerView = ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).getHeaderView();
                ((HeaderAndFooterRecyclerViewAdapter) outerAdapter).removeFooterView(headerView);
            }
        }
    }

    // getLayoutPosition() & getAdapterPosition() are not ported as their mapping are not available
}